package com.sbl.bajw.pc.action;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.WebContext;
import com.sbl.bajw.pc.model.PrivilegeModel;
import com.sbl.bajw.pc.service.PrivilegeService;

@Controller
@RequestMapping("/Privilege")
public class PrivilegeAction {
	
	@Resource
	private PrivilegeService service;
	
	@RequestMapping(value = "/listByRoleDialog")
	public ModelAndView listByRoleDialog(){
		ModelAndView mav = new ModelAndView("/view/manager/privilege/list");
		return mav;
	}
	
	@RequestMapping(value = "/main")
	public ModelAndView Index(){
		ModelAndView mav = new ModelAndView("/view/manager/privilege/main");
		return mav;
	}
	
	@RequestMapping(value = "/list")
	public @ResponseBody String list(){
		String result = service.list();
		return result;
	}
	
	@RequestMapping(value = "/mainDialog")
	public ModelAndView mainDialog(){
		String id = RequestUnit.getString("id");
		if(id != null){
			PrivilegeModel model = service.getModel(id);
			WebContext.getRequest().setAttribute("model", model);
		}
		ModelAndView mav = new ModelAndView("/view/manager/privilege/mainDialog");
		return mav;
	}
	
	@RequestMapping(value = "/add")
	public @ResponseBody String add() throws MessageException{
		PrivilegeModel model = RequestUnit.getModel(PrivilegeModel.class);
		service.insert(model);
		return JsonResultUtil.Success();
	}
	
	@RequestMapping(value = "/delete")
	public @ResponseBody String delete() throws MessageException{
		String id = RequestUnit.getString("id");
		service.delete(id);
		return JsonResultUtil.Success();
	}
	
	@RequestMapping(value = "/update")
	public @ResponseBody String update() throws MessageException{
		PrivilegeModel model = RequestUnit.getModel(PrivilegeModel.class);
		service.update(model);
		return JsonResultUtil.Success();
	}
	
	@RequestMapping(value = "/listByRole")
	public @ResponseBody String listByRole(){
		String result = service.listByRole();
		return JsonResultUtil.Success(result);
	}
	
	@RequestMapping(value = "/addPrivilegeToRole")
	public @ResponseBody String addPrivilegeToRole(){
		service.addPrivilegeToRole();
		return JsonResultUtil.Success();
	}
}
